<?PHP
##################################
/* 配送录入 */
##################################

class Admin_source_send_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','溯源管理 - 配送录入');
		$this->assign('menuid1','7');
		$this->assign('menuid2','3');
		//权限判断
		factory::module('admin')->authSys('source_make');
	}
	function index__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))			 $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['status']))    $_POST['status']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			
  	  #查询条件
			$sql_where = ' where b.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.code' 	,'a.senddate' ));  
			if($_POST['status']!=''){
				$sql_where .= ' and a.status= "'.$_POST['status'].'"';	
			}
			if($_POST['b_date']!=''){
				$sql_where .= ' and a.senddate >= "'.$_POST['b_date'].'"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and a.senddate <= "'.$_POST['e_date'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(b.id) 
			from t_source b
			left join `t_sendpi` a on b.sendpi_id=a.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select 
					b.*,
					a.code,
					a.senddate,
					a.sendfennum,
					a.status
				from t_source b
				left join `t_sendpi` a on b.sendpi_id=a.id  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$v){
					$v['vegenums'] = $db->loadresult('select count(*) from t_source_vege where source_id="'.$v['id'].'" ');
					$v['usernums'] = $db->loadresult('select count(*) from t_source_user where source_id="'.$v['id'].'" ');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}
	}
	/* 供应溯源录入 */
	public function modify__(){
		$_GET=format::addslash_deep($_GET);
		$source_id = intval($_GET['id']);
		$db = factory::getDBO();
	  # 溯源信息
		$rs_source = $db->loadassoc('select 
					b.*,
					a.code,
					a.senddate,
					a.sendfennum,
					a.status
				from t_source b
				left join `t_sendpi` a on b.sendpi_id=a.id  
				where b.id="'.$source_id.'" and b.deleted=0 ');
		if(!$rs_source){
			url::msgBox('没有查到任何记录！','-1');
		}
		#配送阶段字段以及信息
		$rs_source_user = factory::module('source')->getSourceUser($source_id);
		
		if(count($rs_source_user) <= 0){
			url::msgBox('当前批次暂未添加任何配送分支！','-1');
		}
	 	#该阶段的溯源新字段
		$pro_fields = factory::module('source')->getField('1');
		$make_fields = factory::module('source')->getField('2');
		$send_fields = factory::module('source')->getField('3');
		$this->assign('rs_source',$rs_source); 
		$this->assign('rs_source_user',$rs_source_user); 
		$this->assign('pro_fields',$pro_fields);
		$this->assign('make_fields',$make_fields);
		$this->assign('send_fields',$send_fields);
	}
  //修改保存
	public function modify_save__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$source_user_obj = format::addslash_json(json_decode( stripslashes($_POST['source_user_obj']) ));
			$field_obj = format::addslash_json(json_decode( stripslashes($_POST['field_obj']) ));
			$_POST=format::addslash_deep($_POST); 
			$source_id = $_POST['source_id'];
			
	//	 debug::show($source_id,$source_user_obj,$field_obj);
			
			$db = factory::getDBO();
			$db->begin();
			# 溯源信息
			$rs_source = $db->loadassoc('select 
						b.*,
						a.code,
						a.senddate,
						a.sendfennum,
						a.status
					from t_source b
					left join `t_sendpi` a on b.sendpi_id=a.id  
					where b.id="'.$source_id.'" and b.deleted=0 ');
			if(!$rs_source){
				exit('没有可保存的对象！');
			}
			# 更新蔬菜溯源
			foreach($source_user_obj as $v){
				$db->update('source_user',$v);
			}
			# 重新插入自定义字段
			foreach($field_obj as $v){
				# 先删除一下
				$db->query('delete from t_field_user_val where source_user_id="'.$v['source_user_id'].'" and field_id="'.$v['field_id'].'" ');
				$db->insert('field_user_val',array(
					'source_user_id'=>$v['source_user_id'],
					'field_id'=>$v['field_id'],
					'field_val'=>$v['field_val']
				));
			}
			# 更新这个批次的溯源状态
			if(!factory::module('source')->update($rs_source['sendpi_id'])){
				exit('系统繁忙，请稍后重试！');
			}
			# 写日志
			factory::module('source')->logs($source_id,factory::module('admin')->getuserid(),'更新配送溯源');
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
		exit();
	}
}
?>